package com.example.graduatetest.view.model;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModel;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.graduatetest.R;
import com.example.graduatetest.activitys.AddClassActivity;
import com.example.graduatetest.activitys.CreateClassActivity;
import com.example.graduatetest.activitys.FindCardActivity;
import com.example.graduatetest.activitys.HomeActivity;
import com.example.graduatetest.activitys.SendCardActivity;
import com.example.graduatetest.adapter.ClassAdapter;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.User;
import com.example.graduatetest.callback.ClassList;
import com.example.graduatetest.databinding.FragmentClassesBinding;
import com.example.graduatetest.model.ClassModel;
import com.example.graduatetest.utils.Constant;

import java.lang.reflect.Method;
import java.util.List;



public class ClassesViewModel extends ViewModel {

    private FragmentClassesBinding binding;
    private Application application;
    private List<Classes> classesList;
    private Activity activity;
    private Handler handler = new Handler();
    private User user;
    public void init(Activity activity, FragmentClassesBinding binding){
        this.binding = binding;
        this.application = activity.getApplication();
        this.activity = activity;
        user = Constant.checkStudentOrTeacher(activity);
        toolbarInit();
        getData();
    }

    //初始化toolbar
    private void toolbarInit(){
        Toolbar classToolbar = binding.toolbar;
        if(user.isStudent()){
            classToolbar.setTitle("我的课程");
        }else{
            classToolbar.setTitle("我的授课");
        }

        classToolbar.inflateMenu(R.menu.class_toolbar_menu);
        //解决Menu Icon显示
        Menu menu = classToolbar.getMenu();
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //给Item设置点击事件
        classToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent;
                switch (item.getItemId()){
                    case R.id.add_class_item:
                        if(user.isStudent()){
                            intent = new Intent(application, AddClassActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            application.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case R.id.create_class_item:
                        if(!user.isStudent()){
                            intent = new Intent(application, CreateClassActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            application.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case R.id.card_class_item:
                        if(user.isStudent()){
                            intent = new Intent(application, FindCardActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("class_number", "NULL");
                            application.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case  R.id.card_up_item:
                        if(!user.isStudent()){
                            intent = new Intent(application, SendCardActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            application.startActivity(intent);
                        }else{
                            Toast.makeText(activity, "你没有权限", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }

                return false;
            }
        });

    }


    //初始化recyclerView
    private void recyclerInit(){
        LinearLayoutManager layoutManager = new LinearLayoutManager(application);
        binding.recyclerViewClass.setLayoutManager(layoutManager);
        if(classesList != null){
            ClassAdapter classAdapter = new ClassAdapter(classesList,activity);
            binding.recyclerViewClass.setAdapter(classAdapter);
        }

    }
    //获取数据
    private void getData(){
        if(user.isStudent()){
            ClassModel.getClassList(user.getNumber(), new ClassList() {
                @Override
                public void onSuccess(List<Classes> list) {
                    classesList = list;
                    Log.e("haha", "请求成功了，大聪明");
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            recyclerInit();
                        }
                    });
                }

                @Override
                public void onFailed() {
                    classesList = null;
                    Log.e("haha", "请求失败了，傻逼");
                }
            });
        }else{
          ClassModel.getClassListTeacher(user.getNumber(), new ClassList() {
              @Override
              public void onSuccess(List<Classes> list) {
                  classesList = list;
                  Log.e("haha", "请求成功了，大聪明");
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          recyclerInit();
                      }
                  });
              }

              @Override
              public void onFailed() {
                  classesList = null;
                  Log.e("haha", "请求失败了，傻逼");
              }
          });
        }

    }

}
